<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2018/11/1
 * Time: 15:40
 */
namespace App\Weixin\Repositories;

use App\Weixin\Contracts\UserInterface;
use App\Weixin\Contracts\WithdrawInterface;
use App\Weixin\Exceptions\HomeException;
use App\Weixin\Models\UserWithdrawLogModel;
use Illuminate\Support\Facades\Request;

/**
 * 微信用户接口
 * Class DbUserRepositories
 * @package App\Weixin\Repositories
 */
class DbWithdrawRepositories implements WithdrawInterface
{
    /**
     * @return UserWithdrawLogModel
     */
    public function getUserWithdrawLogModel(){
        return app()->make(UserWithdrawLogModel::class);
    }

    /**
     * 用户接口
     * @return UserInterface
     */
    public function getUserInterface(){
        return app()->make(UserInterface::class);
    }

    /**
     * 获取列表数据
     * @param $where
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getList($where){

        $request=Request::instance();
        $limit=empty($request->input("limit"))?10:$request->input("limit");
        $sort=empty($request->input("sort"))?'create_time':$request->input("sort");
        $order=empty($request->input("order"))?'asc':$request->input("order");
        return $this->getUserWithdrawLogModel()->newQuery()->where($where)->orderBy($sort,$order)->paginate($limit);
    }

    /**
     * 更新数据
     * @param $where
     * @param $data
     * @return int
     */
    public function updateData($where,$data){
        return $this->getUserWithdrawLogModel()->newQuery()->where($where)->update($data);
    }

    /**
     * 添加提现操作
     */
    public function addWithdrawLog($data){
         $userInfo=$this->getUserInterface()->getUserInfoById(AUID);
         $isFull=get_config_param_value("WITHDRAW_BALANCE_ISFULL");//金额是否已达提现金额
        $poundage=$this->getUserWithdrawLogModel()->getPoundageByAmount($data['amount']);
        $makeAmount=$data['amount']+$poundage;//此次提现总余额支付总金额
         if($makeAmount<$isFull){
              throw new HomeException(HomeException::EROOR_WITHDRAW_BALANCE_ISFULL,['msg'=>$isFull]);
         }
         if($userInfo->balance<$makeAmount){
             throw new HomeException(HomeException::EROOR_WITHDRAW_BALANCE_SHORTAGE);
         }
         if($makeAmount<0){
             throw new HomeException(HomeException::EROOR_WITHDRAW_BALANCE_MAX);
         }
         /**
          * 将提现放到提现记录上
          */
        $data['order_sn']=$this->makePaySn(AUID); //订单号
        $data['poundage']=$poundage; //手续费
        $data['uid']=AUID;
         if($rInfo=$this->getUserWithdrawLogModel()->newQuery()->create($data)){
                 //修改用户提现帐号
                 $this->getUserInterface()->changeStatus(["uid"=>AUID],["wx_wechat_account"=>$data["account"]]);
                return $this->getUserInterface()->updateUserBalance(AUID,-$makeAmount,UserWithdrawLogModel::BALANCE_TYPE_1,$rInfo->id);
         }else{
           throw  new HomeException(HomeException::EROOR_WITHDRAW_BALANCE_LOG);
         }
    }
    /**
     * 生成支付单编号(两位随机 + 从2000-01-01 00:00:00 到现在的秒数+微秒+会员ID%1000)，该值会传给第三方支付接口
     * 长度 =2位 + 10位 + 3位 + 3位  = 18位
     * 1000个会员同一微秒提订单，重复机率为1/100
     * @return string
     */
    function makePaySn($member_id)
    {
        return mt_rand(10, 99)
            . sprintf('%010d', time() - 946656000)
            . sprintf('%03d', (float)microtime() * 1000)
            //. sprintf('%03d', (int) $member_id % 1000);
            . $member_id;
    }

    /**
     * 进行退款操作
     * @param $where
     * @param $data
     */
    public function balckUserData($where,$data){
        if($this->getUserWithdrawLogModel()->newQuery()->where($where)->update($data)){
            $uwlModel=$this->getUserWithdrawLogModel()->newQuery()->where($where)->first();
            $makeAmount=$uwlModel->amount+$uwlModel->poundage;
            return $this->getUserInterface()->updateUserBalance($uwlModel->uid,$makeAmount,UserWithdrawLogModel::BALANCE_TYPE_1,$uwlModel->id);
        }else{
            throw  new HomeException(HomeException::EROOR_WITHDRAW_BALANCE_OPTION);
        }

    }

    /***
     * 获取提现详细
     * @param $where
     * @return mixed|void
     */
    public function getInfo($where){
         return $this->getUserWithdrawLogModel()->newQuery()->with(["user"])->where($where)->first();
    }

    /**
     * 获取提现手续费
     * @param $amount
     * @return mixed|void
     */
    public function getPoundage($amount){
        return $this->getUserWithdrawLogModel()->getPoundageByAmount($amount);
    }
}